﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Xml.XPath;

namespace Songhay.Data.GenericWeb.Procedures
{
    using Songhay.Data.Common;
    using Songhay.Data.GenericWeb.DataTransferContracts;
    using Songhay.Data.GenericWeb.Properties;

    /// <summary>
    /// Update procedures into the DBMS.
    /// </summary>
    public static partial class Update
    {
        /// <summary>
        /// Updates the specified Segment data into the DBMS.
        /// </summary>
        /// <param name="commonConnection">The Common connection.</param>
        /// <param name="segment">The Segment data according to the data transfer contract.</param>
        /// <returns>Returns the unique identifier of the updated item.</returns>
        public static int? Segment(DbConnection commonConnection, ISegment segment)
        {
            if(commonConnection == null) throw new ArgumentNullException("commonConnection", "The Common Connection is null.");
            if(segment == null) throw new ArgumentNullException("segment", "The Input Segment is null.");

            Dictionary<string, object> paramSet = new Dictionary<string, object>();
            paramSet.Add("@SegmentId", segment.SegmentId);
            paramSet.Add("@SegmentName", segment.SegmentName);
            paramSet.Add("@SortOrdinal", segment.SortOrdinal);
            paramSet.Add("@CreateDate", segment.CreateDate);
            paramSet.Add("@ParentSegmentId", segment.ParentSegmentId);
            paramSet.Add("@ClientId", segment.ClientId);
            paramSet.Add("@IsActive", segment.IsActive);

            using(DbCommand cmd = commonConnection.CreateCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = Resources.SetSegment;

                DbParameter[] paramArray = CommonParameter.GetParameters(cmd, paramSet);
                cmd.Parameters.AddRange(paramArray);

                cmd.ExecuteNonQuery();

                return FrameworkType.ParseInt32(paramSet["@SegmentId"].ToString());
            }
        }
    }
}
